package com.voxel.sense.infomsg.module.task;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.onMyItemClickListener;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.response.MyTaskListInfoEntity;


/**
 * Created by ChenYiYao on 2017/12/3.
 */

public class TaskListAdapter extends RecyclerAdapter<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> {
    private int BtnBgResid = -1; //按钮的背景资源
    private String mType = Constants.TASK_ALL_TYPE;
    private onMyItemClickListener mListener;

    public TaskListAdapter(int type) {
        this();
//        this.mType = type;
    }

    public TaskListAdapter() {
        super();
    }


    @Override
    protected int getItemViewType(int position, MyTaskListInfoEntity.BodyBean.TaskInfoListBean myTaskListInfoEntity) {
        return R.layout.item_recycler_misson_manager;
    }


    @Override
    protected ViewHolder onCreateViewHolder(View root, int viewType) {

        return new ViewHolder(root, mType);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> holder, final int position) {
        super.onBindViewHolder(holder, position);
//任务状态改变按钮事件
        View itemView = holder.itemView;
        itemView.findViewById(R.id.btn_action).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onItemClick(position);
                }
            }
        });
    }


    public static class ViewHolder extends RecyclerAdapter.ViewHolder<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> {
        private String mType = Constants.TASK_ALL_TYPE;
        @BindView(R.id.tv_desc)
        TextView tv_desc; //任务描述
        @BindView(R.id.tv_type)
        TextView tv_type;//任务紧急状态描述
        @BindView(R.id.tv_firm_name)
        TextView tv_firm_name;//公司名称
        @BindView(R.id.iv_location_icon)
        ImageView iv_location_icon;//定位坐标
        @BindView(R.id.tv_district)
        TextView tv_district;//所在区区
        @BindView(R.id.tv_address)
        TextView tv_address;//详细地址
        @BindView(R.id.iv_time_icon)
        ImageView iv_time_icon;//时间的icon
        @BindView(R.id.tv_start_time)
        TextView tv_start_time;//开始时间
        @BindView(R.id.tv_people_name)
        TextView tv_people_name;//执行人
        @BindView(R.id.tv_status)
        TextView tv_status;//任务执行状态
        @BindView(R.id.btn_action)
        Button btn_action;//改变状态按钮

        public ViewHolder(View itemView, String type) {
            super(itemView);
            this.mType = type;
        }

        @Override
        protected void onBind(MyTaskListInfoEntity.BodyBean.TaskInfoListBean data) {
            tv_desc.setText(data.getTaskType());
            if (!TextUtils.isEmpty(data.getTaskUrgentGrade())) {
                tv_type.setText(data.getTaskUrgentGrade());
                tv_type.setVisibility(View.VISIBLE);
            } else {
                tv_type.setVisibility(View.GONE);
            }

            String id = data.getTaskStatusId();
            if (mType == Constants.TASK_ALL_TYPE) {
                tv_status.setVisibility(View.VISIBLE);
                if (id.equals(Constants.TASK_UNACCEPT_TYPE + "")) {
                    tv_status.setText("未接收");
                    tv_status.setBackgroundResource(R.drawable.shape_task_unaccept_status_bg_bg);
                } else if (id.equals(Constants.TASK_FINISH_TYPE + "")) {
                    tv_status.setText("已执行");
                    tv_status.setBackgroundResource(R.drawable.shape_task_finish_status_bg_bg);
                } else if (id.equals(Constants.TASK_ACTION_TYPE + "")) {
                    tv_status.setText("已接收");
                    tv_status.setBackgroundResource(R.drawable.shape_task_accept_status_bg_bg);
                } else if (id.equals(Constants.TASK_CHECK_PASS + "")) {
                    tv_status.setText("审核通过");
                    tv_status.setBackgroundResource(R.drawable.shape_task_accept_status_bg_bg);
                }
            } else {
                tv_status.setVisibility(View.GONE);
            }
            tv_firm_name.setText(data.getTaskObject());
            tv_district.setText(data.getTaskAreaName());
            tv_address.setText(data.getTaskObjectAddress());
            tv_people_name.setText(data.getTaskExcutorName());
            tv_start_time.setText(data.getTaskStartTime() + " - " + data.getTaskEndTime());
            //判断类型，是不是显示改变任务状态的点击按钮
            if (mType == Constants.TASK_UNACCEPT_TYPE) {//未接收
                btn_action.setVisibility(View.VISIBLE);
                btn_action.setText(Factory.app().getResources().getString(R.string.accept_task));
                btn_action.setBackground(Factory.app().getResources().getDrawable(R.drawable.shape_task_btn_accept_bg));
            } else if (mType == Constants.TASK_FINISH_TYPE) {//执行完成审核状态
                btn_action.setVisibility(View.VISIBLE);
                btn_action.setText(Factory.app().getResources().getString(R.string.audit_task));
                btn_action.setBackground(Factory.app().getResources().getDrawable(R.drawable.shape_task_btn_bg));
            } else if (mType == Constants.TASK_ACTION_TYPE) {//执行中
                btn_action.setVisibility(View.VISIBLE);
                btn_action.setText(Factory.app().getResources().getString(R.string.action_task));
                btn_action.setBackground(Factory.app().getResources().getDrawable(R.drawable.select_task_btn_action_bg));
            } else {
                btn_action.setVisibility(View.GONE);
            }
        }

        public int getType() {
//            return mType;
            return 0;
        }

        public void setType(int type) {
//            this.mType = type;
        }
    }

    public void setBtnBgResid(int btnBgResid) {
        BtnBgResid = btnBgResid;
    }

    public void setOnchangStatuBtn(onMyItemClickListener listener) {
        mListener = listener;
    }
}
